package 链表.单向.练习;
//反转列表


class ListNode{
    public int val;
    public ListNode next;//next指向新的节点

    public ListNode(int val) {
        this.val = val;
    }
}
public class Text_1 {
    public static ListNode head;
    static void reverse(){
        if(head==null){
            return ;
        }
        if(head.next==null ){
            return ;
        }

        ListNode cur = head.next;
        //少这一步，会变成循环节点
        head.next =null;

        while(cur!= null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
            //可以理解为每次移动一个，curNext起到了一个存储位置的作用
            //每次都是head和cur交换位置
        }
    }
    public void addFirst(int data){
        //代码不能交换
        //必须先绑定信息
        ListNode listNode = new ListNode(data);
        listNode.next = head;
        head = listNode;
    }
    void display(){
        ListNode cur = head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;//cur向后走一步
        }
        System.out.println();
    }


    public static void main(String[] args) {
        Text_1 text1 = new Text_1();
        text1.addFirst(1);
        text1.addFirst(2);
        text1.addFirst(3);
        text1.display();
        text1.reverse();
        text1.display();

    }
}
